<template>
  <div class="home">
    <div class="search">
      <el-input placeholder="请输入名称进行搜索" v-model="searchName" class="input-with-select">
        <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
      </el-input>
    </div>
    <ul class="item">
      <li><p>类型</p></li>
      <li><p :class="category === '' ? 'now' : ''" @click="changeCategory('')">全部</p></li>
      <li v-for="(item,index) in categoryList" :key="index"><p :class="category == item ? 'now' : ''" @click="changeCategory(item)">{{item}}</p></li>
    </ul>
    <ul class="item">
      <li><p>年份</p></li>
      <li><p :class="year === '' ? 'now' : ''" @click="changeYear('')">全部</p></li>
      <li v-for="(item,index) in yearList" :key="index"><p :class="year == item ? 'now' : ''" @click="changeYear(item)">{{item}}</p></li>
    </ul>
    <ul class="item">
      <li><p>地区</p></li>
      <li><p :class="country === '' ? 'now' : ''" @click="changeCountry('')">全部</p></li>
      <li v-for="(item,index) in countryList" :key="index"><p :class="country === item ? 'now' : ''" @click="changeCountry(item)">{{item}}</p></li>
    </ul>
    <div class="box">
      <div v-for="(item,index) in moiveList" :key="index">
        <div class="picture">
          <img width="150" :src="item.picture" @click="toDetail(item)">
        </div>
        <div class="name">
          <p>{{item.title}}</p>
        </div>
        <div class="content">
          <p>{{item.content}}</p>
        </div>
      </div>
    </div>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="10"
      class="page"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<style lang="scss" scoped>

.search {
  width: 500px;
  margin: 0 auto;
  position: relative;
  top: 15px;
}
.item {
  width: 1600px;
  min-width: 1600px;
  height: 50px;
  position: relative;
  top: 30px;
  display: flex;
  flex-wrap: wrap;
  margin: 0 auto;
  li {
    list-style: none;
    width: 100px;
    height: 50px;
    display: flex;
    p {
      margin: auto;
      cursor: pointer;
    }
  }
  .now {
    color: #ffb600;
  }
}
.box {
  width: 1600px;
  min-width: 1600px;
  height: 600px;
  margin: 0 auto;
  position: relative;
  top: 40px;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-start;
  &>div {
    width: 320px;
    height: 300px;
    .picture {
      width: 300px;
      height: 240px;
      display: flex;
      img {
        margin: auto;
        cursor: pointer;
      }
    }
    .name {
      width: 300px;
      height: 25px;
      font-weight: bold;
      p {
        text-align: center;
        margin: auto;
      }
    }
    .content {
      width: 300px;
      height: 30px;
      p {
        text-align: center;
        margin: auto;
      }
    }
  }
}
.page {
  position: relative;
  left: 780px;
  top: 80px;
}
.el-pagination {
  width: 500px;
}
</style>

<script>

export default {
  metaInfo: {
    meta: [
      { charset: 'utf-8' },
      { name: 'referrer', content: 'never' }
    ]
  },
  data() {
    return {
      searchName: "",
      categoryList: ["剧情","动作","喜剧","爱情","犯罪","奇幻","动画","科幻","悬疑","冒险","历史","战争","音乐","古装"],
      yearList: ["2019","2018","2017","2016","2015","2014","2013","2012","2011","2010","2009~2000","90年代及以前"],
      countryList: ["中国","美国","英国","法国","日本","德国","韩国"],
      category: "",
      year: "",
      country: "",
      currentPage: 1,
      total: 100,
      moiveList: [],
    }
  },
  methods: {
    async search() {
      this.currentPage = 1;
      const res = await this.$http.get("/getData",{
        params: {
          title: this.searchName,
          pageNum: this.currentPage
        }
      });
      this.moiveList = res.data.data;
      this.total = res.data.total;
    },
    changeCategory(category) {
      this.category = category;
      this.currentPage = 1;
      this.searchName = "";
      this.fetchData();
    },
    changeYear(year) {
      this.currentPage = 1;
      this.searchName = "";
      this.year = year;
      this.fetchData();
    },
    changeCountry(country) {
      this.country = country;
      this.currentPage = 1;
      this.searchName = "";
      this.fetchData();
    },
    showPicture() {
      this.moiveList.forEach(item => {
        const result = item.picture.split("jpg");
        item.picture = result[0] + "webp";
      });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.fetchData();
    },
    toDetail(item) {
      console.log(item);
      localStorage._id = item._id;
      localStorage.category = this.category;
      localStorage.country = [item.country.split(" ")][0];
      this.$router.push(`/detail?${item._id}`);
    },
    async fetchData() {
      this.category = this.category.length ? this.category : "";
      this.country = this.country.length ? this.country : "";
      let year = this.year.length ? this.year: "";
      if(year == "2009~2000") {
        year = "2005"
      }
      if(year == "90年代及以前") {
        year = "1995"
      }
      const res = await this.$http.get("/getData",{
        params: {
          category: this.category,
          country: this.country,
          year,
          pageNum: this.currentPage
        }
      });
      this.moiveList = res.data.data;
      this.total = res.data.total;
    }
  },
  created() {
    this.fetchData();
  }
}
</script>
